/*
 *基础界面:点击主干栏修改页面
 */
function focusMainSidebar(obj) {
	$(".mainSidebar.nav-link.active").attr("class", "mainSidebar nav-link");
	obj.className = "mainSidebar nav-link active";
};
/*
 * 基础界面:点击主分支栏修改页面
 */
function focusBranchSidebar(obj) {
	var mainBranch = obj.parentNode.parentNode.parentNode.children[0].className;
	$(".mainSidebar.nav-link.active").attr("class", "mainSidebar nav-link");
	obj.parentNode.parentNode.parentNode.children[0].className = "mainSidebar nav-link active";
	$(".branchSidebar.nav-link.active").attr("class", "branchSidebar nav-link");
	obj.className = "branchSidebar nav-link active";
	$("#pageButtonGroup").empty();
	context = "<button type='button' class='btn btn-secondary'>1</button>";
	$("#pageButtonGroup").append(context)
};
/*
 * 业务1:通过id搜索用户 切换展示页面
 */
function showSearchByAccountId() {
	$(".card-body").empty();
	$(".card-body")
			.append(
					"<div id='account-div'><div class='input-group mb-3' style='width: 400px;'><input type='text' class='form-control' name='accountId'placeholder='请输入想查询的账户Id' id='accountId' /><div class='input-group-append'><button class='btn btn-outline-secondary' type='button'id='accountById-button' onclick='accountByAccountId()'>查询</button></div></div></div>");
}
/*
 * 业务1:发送请求，提交用户id，刷新界面
 */
function accountByAccountId() {
	var accountId = $("#accountId").val();
	$(".card-body").empty();
	$
			.ajax({
				url : "findAccountById",
				type : "post",
				async : true,
				dataType : "json",
				data : {
					"accountId" : accountId,
				},
				success : function(account) {
					context = "<table class='table'><thead class='thead-dark'><tr><th>id</th><th>姓名</th><th>性别</th><th>账户地址</th><th>账户余额</th></tr></thead><tbody><tr><td>"
							+ account.accountId
							+ "</td><td>"
							+ account.accountName
							+ "</td><td>"
							+ account.accountGender
							+ "</td><td>"
							+ account.accountAddress
							+ "</td><td>"
							+ account.accountBalance
							+ "</td></tr></tbody></table>";
					$(".card-body").append(context);
				},
				error : function(error) {
					console.log("错误！");
					context = "<div class='d-inline p-2 bg-primary text-white'>该用户不存在！</div>";
					$(".card-body").append(context);
				}
			});
};

/*
 * 业务2:展示所有用户
 */
function accountList(pageNumber) {
	var pageSize = 5;
	var currentPage = 1;
	$(".card-body").empty();
	$
			.ajax({
				url : "listAccounts",
				type : "post",
				async : true,
				dataType : "json",
				data : {"pn":pageNumber},
				success : function(account) {
					context = "<table class='table'><thead class='thead-dark'><tr><th>id</th><th>姓名</th><th>性别</th><th>账户地址</th><th>账户余额</th></tr></thead><tbody>";
					$.each(account, function(index, account) {
						context += "<tr><td>" + account.accountId + "</td><td>"
								+ account.accountName + "</td><td>"
								+ account.accountGender + "</td><td>"
								+ account.accountAddress + "</td><td>"
								+ account.accountBalance + "</td></tr>";
					});
					context += "</tbody></table>";
					$(".card-body").append(context);
					$
							.ajax({
								url : "countAccounts",
								type : "post",
								async : true,
								dataType : "json",
								data : {},
								success : function(count) {
									$("#pageButtonGroup").empty();
									for (var i = 1; i <= (count / pageSize) + 1; i++) {
										if (i == 1) {
											context = "<button type='button' class='btn btn-secondary' onclick='accountList("+i+")'>"
													+ i + "</button>";
										} else {
											context += "<button type='button' class='btn btn-secondary' onclick='accountList("+i+")'>"
													+ i + "</button>";
										}
									}
									$("#pageButtonGroup").append(context);

								}
							});

				},
				error : function(error) {
					console.log("错误！");
					context = "<div class='d-inline p-2 bg-primary text-white'>数据库中不存在任何账户！</div>";
					$(".card-body").append(context);
				}
			});
};
